#include "TCPSever.hpp"
#include "Protocol.hpp"
#include "Calculator.hpp"

std::string Work(std::string& package)
{
    std::string message;
    std::string ret;
    //解包，循环获取直到不能解析为止
    while(Decode(package,&message))
    {
        if(message.empty()) 
            break;
        //反序列化
        Request req;
        if(!req.Deserialize(message))
            break;
        //计算结果
        Response res=Calculator().Execute(req);

        //序列化
        res.Serialize(message);

        //封包
        EnCode(message,&message);
        //添加到结果缓存
        ret+=message;
    }
    return ret;
}

int main()
{

    std::unique_ptr<TCPSever> ts_ptr = std::make_unique<TCPSever>(Work);
    ts_ptr->Run();
    return 0;
}